package edu.scau.mis.web.controller.pos;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.mis.core.annotation.RequestLog;
import edu.scau.mis.core.domain.AjaxResult;
import edu.scau.mis.core.enums.BusinessType;
import edu.scau.mis.pos.domain.Product;
import edu.scau.mis.pos.service.IProductService;
import edu.scau.mis.pos.service.IStoreService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Autowired
    private IStoreService storeService;

    @RequestLog(title = "查询所有产品",businessType = BusinessType.Query)
    @GetMapping("/listAll")
    public AjaxResult listAll(){
        return AjaxResult.success(productService.listAllProduct());
    }

    @RequestLog(title = "查询所有产品",businessType = BusinessType.Query)
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Product product) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productService.selectProductByPage(product);
        PageInfo<List> pageInfo = new PageInfo(list);

        // 以下写法也可以
        // PageInfo<ProductDescription> pageInfo = new PageInfo<>(list)
        return AjaxResult.success(pageInfo);
    }
    @RequestLog(title = "根据编码查询产品",businessType = BusinessType.Query)
    @GetMapping("/selectBySn")
    public AjaxResult selectProductBySn(@RequestParam("productSn") String productSn){
        return AjaxResult.success(productService.selectProductBySn(productSn));
    }
    @RequestLog(title = "根据价格区间查询产品",businessType = BusinessType.Query)
    @GetMapping("/selectByPrice")
    public AjaxResult selectProductByPrice(@RequestParam("minPrice") Integer minPrice, @RequestParam("maxPrice") Integer maxPrice) {
        return AjaxResult.success(productService.selectProductByPrice(minPrice,maxPrice));
    }
    @RequestLog(title = "根据名字查询产品",businessType = BusinessType.Query)
    @GetMapping("/selectByName")
    public AjaxResult selectProductByName(@RequestParam("productName") String productName) {
        return AjaxResult.success(productService.selectProductByName(productName));
    }
    @RequestLog(title = "添加产品",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addProduct(@RequestBody Product product) {
        if (product.getCategoryId() == null) {
            return AjaxResult.error("商品类别不能为空");
        }

        // 判断该商品是否有上架的 SKU
        if (!storeService.hasAvailableSku(product.getProductSn())) {
            return AjaxResult.error("该商品未上架，无法录入");
        }

        // 强制转换 categoryId 为 String
        String categoryName = productService.getCategoryNameById(String.valueOf(product.getCategoryId()));
        product.setCategoryName(categoryName);

        product.setProductSn(null); // 不用前端传的编码
        int rows = productService.addProduct(product);
        return rows > 0 ? AjaxResult.success("新增商品成功，编码：" + product.getProductSn()) : AjaxResult.error("新增商品失败");
    }



    @RequestLog(title = "删除产品",businessType = BusinessType.DELETE)
    // 单个删除
    @DeleteMapping("/deleteBySn")
    public AjaxResult deleteProductBySn(@RequestParam("productSn") String productSn) {
        int rows = productService.deleteProductBySn(productSn);
        return rows > 0 ? AjaxResult.success("删除商品成功") : AjaxResult.error("删除商品失败");
    }
    @RequestLog(title = "批量删除产品",businessType = BusinessType.DELETE)
    // 批量删除
    @DeleteMapping("/deleteBySnBatch")
    public AjaxResult deleteProductBySnBatch(@RequestBody List<String> productSnList) {
        int rows = productService.deleteProductBySnBatch(productSnList);
        return rows > 0 ? AjaxResult.success("批量删除商品成功") : AjaxResult.error("批量删除商品失败");
    }
    @RequestLog(title = "更新产品",businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public ResponseEntity<String> updateProduct(@RequestBody Product product) {
        if (product.getCategoryName() == null || product.getCategoryName().isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("商品类别不能为空");
        }
        int result = productService.updateProduct(product);
        if (result > 0) {
            return ResponseEntity.ok("商品更新成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("商品更新失败");
        }
    }

    @RequestLog(title = "根据编码查询产品",businessType = BusinessType.Query)
    @GetMapping("/getBySn/{productSn}")
    public AjaxResult getProductBySn(@PathVariable String productSn) {
        return AjaxResult.success(productService.getProductBySn(productSn));
    }






}
